// JavaScript Document
FF.Console = {
	
	Console: false,
	CommandHistory: new Array(),
	CommandPointer: false,
	ViewType: 'inpage',
	
	createConsole: function() {
		var Console = Builder.node('DIV', {id: 'Console'}, [
			Builder.node('A', {style: 'cursor: pointer; display: block; padding: 4px; font-size: 11px; color: #fff; font-weight: bold; text-align:right', onclick: 'FF.Console.hide();window.open("Console.php", "newconsole", "width=500,height=550")'}, 'Popup VIEW'),
			Builder.node('DIV', {className: 'ConsoleContainer'}, [
				Builder.node('DIV', { id: 'consoleShower'}),
				Builder.node('INPUT', { id: 'consoleInput', value: '> '})
			])
		]);
		document.body.appendChild(Console);
		this.Console = Console;
	},
	
	show: function() {
		if(this.Console == false) this.createConsole();
		new Effect.Move($('Console'), {y: 0, x: 150, duration: .5, mode:'absolute', transition: Effect.Transitions.sinoidal, afterFinishInternal: function() {
			FF.Console.addMessage("Console Ready!");
			$('consoleInput').focus();
			$('consoleInput').observe('keypress', FF.Console.manageKey);
		}});
		Element.setOpacity($('Console'), .8);
	},
	
	hide: function() {
		if (this.ViewType == 'inpage') {
			new Effect.Move($('Console'), {
				y: -350,
				x: 150,
				duration: .5,
				mode: 'absolute',
				transition: Effect.Transitions.sinoidal,
				afterFinishInternal: function(){
					$('consoleInput').stopObserving('keypress', FF.Console.manageKey);
				}
			});
		} else {
			window.close();
		}
	},
	
	getCommandFromHistory: function(v) {
		if(this.currentCommand === false) this.currentCommand = 1;
		else this.currentCommand++;
		var cmd = this.CommandHistory[(this.CommandHistory.length * v) - this.currentCommand];
		$('consoleInput').innerHTML = "> " + cmd;
	},
	
	manageKey: function(event) {
		if(event.keyCode == Event.KEY_RETURN) {
			FF.Console.run(event.element().value);
		} else if(event.keyCode == Event.KEY_UP) {
			FF.Console.getCommandFromHistory(-1);
		} else if(event.keyCode == Event.KEY_DOWN) {
			FF.Console.getCommandFromHistory(1);
		}
	},
	
	storeCommand: function(cmd) {
		this.CommandHistory.push(cmd);
		this.CommandPointer = false;
	},
	
	run: function(cmd) {
		var cmd = this.parseCommand(cmd);
		this.storeCommand(cmd);
		this.addMessage(cmd);
		$('consoleInput').value = "";
		$('consoleInput').readOnly = true;
		FF.Console.Commands(cmd);
	},
	
	resetCursor: function() {
		$('consoleInput').readOnly = false;
		$('consoleInput').value = "> ";
		$('consoleInput').focus();
	},
	
	addMessage: function(msg, system) {
		if(!system) var pre = "> ";
		else var pre = "";
		$('consoleShower').innerHTML += pre + msg + "<br>";
		this.resetCursor();
	},
	
	parseCommand: function(cmd) {
		if(cmd[0] == ">" && cmd[1] == " ") return cmd.split("> ")[1];
	},
	
	
	Commands: function(extCommand) {
		var cmd = extCommand.split(" ")[0];
		params = extCommand.split(" ").without(cmd).join(" ");
		
		switch(cmd) {
			
			case 'clear':
				if(params != null && params != "") {
					if(params == 'log') {
						new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=clearLog', {
							method: 'get',
							onComplete: function(ajax) {
								FF.Console.addMessage(ajax.responseText, true);
							}
						});
					} else if(params == 'errorlog') {
						new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=clearerrorlog', {
							method: 'get',
							onComplete: function(ajax) {
								FF.Console.addMessage(ajax.responseText);
							}
						});
					}
				} else {
					$('consoleShower').innerHTML = "";
					this.resetCursor();
				}
				break;
			
			case 'exit':
				this.addMessage("BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE BYE<br>", true);
				this.hide();
				break;
			
			case 'refresh':
				if(params != null && params != "") {
					if(params == 'ALL') parent.window.location.reload();
					if(params == 'menu') parent.menu.window.location.reload();
					if(params == 'header') parent.header.window.location.reload();
					this.addMessage(params + " reloaded", true);
				} else 	window.location.reload();
				break;
				
			case 'query:':
				new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=query&params='+params, {
					method: 'get',
					onComplete: function(ajax) {
						FF.Console.addMessage(ajax.responseText, true);
					}
				});
				break;
				
			case 'get:':
				new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=get&query='+params, {
					method: 'get',
					onComplete: function(ajax) {
						FF.Console.addMessage(ajax.responseText);
					}
				});
				break;
				
			case 'setValue:':
				if(params.indexOf(', ') != -1) {
					var value = params.split(", ")[1];
					new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=setValue&query='+params.split(", ")[0]+'&value='+params.split(", ")[1], {
						method: 'setValue',
						onComplete: function(ajax) {
							FF.Console.addMessage(ajax.responseText);
						}
					});
				} else {
					FF.Console.addMessage("Comando errato. Manca il valore.");
				}
				break;
			
			case 'log':
				new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=log', {
					method: 'get',
					onComplete: function(ajax) {
						FF.Console.addMessage(ajax.responseText);
					}
				});
				break;
				
			case 'errorlog': 
				new Ajax.Request('inc/ajaxRequests.php?ConsoleCmd=errorlog', {
					method: 'get',
					onComplete: function(ajax) {
						FF.Console.addMessage(ajax.responseText);
					}
				});
				break;
				
			case 'help':
				this.addMessage("** HELP **<br><br>'clear': Pulisce la console<br><br>'refresh': Ricarica la pagina corrente<br><br>'exit': Chiude la console mantenendo i messaggi<br><br>'query: +arguments': Effettua una query a DB e stampa un dump del risultato. Importante scrivere 'query:' seguito da uno spazio e dalla query vera e propria<br><br>'log': Stampa il log dell'FF Admin<br>", true);
				break;
			
			default:
				this.addMessage("Comando non conosciuto!", true);
				break;
			
		}
	}
	
	
}
